var express = require('express');
var app = express();
var ejs = require('ejs');
var fs = require('fs');
var path = require('path');
var multer  = require('multer');
var upload = multer({ dest: 'uploads/' });

var default_port = 8848;

app.use('favicon.ico', function (req, res) {
    res.end();
});

app.use(/\/src\/(.+\.html)$/, function (req, res) {
    var filename = path.resolve(__dirname, 'src/html', req.params[0]);
    var content = fs.readFileSync(filename, 'utf8');
    res.end(ejs.render(content, {}, {filename}));
});

app.use('/src', express.static('./src/', { redirect:false }));
app.use('/src/assets/img', express.static('./src/img/', { redirect:false }));
app.use('/src/assets/css', express.static('./src/css/', { redirect:false }));
app.use('/src/assets/js', express.static('./src/js/', { redirect:false }));
app.use('/src/assets/plugins', express.static('./src/assets/plugins/', { redirect:false }));

app.post('/upload', upload.single('avatar'), function (req, res, next) {
    res.json({
        status: true,
        message: '上传成功'
    });
});

app.use('/', express.static('./dist/', { redirect:false }));

app.use('*', require('./index'));



var index = process.argv.indexOf('--port');
var port = index > -1 ? (process.argv[index + 1] || default_port) : default_port;

app.listen(port, function () {
    console.log('Server start at http://localhost:%s', port);
});